package org.video.mine.mycode;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import org.video.mine.R;

public class SpeedPopWindow{
    SpeedClick speedClick;
    PopupWindow popupWindow;
    Context context;

    public SpeedPopWindow(Context context) {
        this.context = context;
    }

    private void showPopupWindow(View ancher) {
        View view = LayoutInflater.from(context).inflate(R.layout.speed_layour,null);
        TextView tv2 = view.findViewById(R.id.tv_2);
        TextView tv15 = view.findViewById(R.id.tv_15);
        TextView tv125 = view.findViewById(R.id.tv_125);
        TextView tv1 = view.findViewById(R.id.tv_1);
        TextView tv075 = view.findViewById(R.id.tv_075);
        TextView tv05 = view.findViewById(R.id.tv_05);

        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(2.0f);
                }
            }
        });
        tv15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(1.5f);
                }
            }
        });
        tv125.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(1.25f);
                }
            }
        });
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(1.0f);
                }
            }
        });
        tv075.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(0.75f);
                }
            }
        });
        tv05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (speedClick!=null){
                    speedClick.speed(0.5f);
                }
            }
        });
        if (popupWindow == null)
            popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        ColorDrawable cd = new ColorDrawable(0x77000000);
        popupWindow.setBackgroundDrawable(cd);

        if (popupWindow != null && !popupWindow.isShowing()) {
            popupWindow.showAsDropDown(ancher,0,0);
        }

    }


    public void dismissPopWindow() {
        if(popupWindow != null){
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    public void show(View view){
        if (popupWindow != null) {
            if (!popupWindow.isShowing()) {
                showPopupWindow(view);
            } else {
                dismissPopWindow();
            }
        } else {
            showPopupWindow(view);
        }
    }

    public void setSpeedClick(SpeedClick speedClick) {
        this.speedClick = speedClick;
    }

    public interface SpeedClick{
        void speed(float speed);
    }

    public boolean isShowing(){
        if (popupWindow!=null){
            if (popupWindow.isShowing()){
                return true;
            }
        }
        return false;
    }
}
